/**
 * @Company: 北京慧点科技有限公司 | www.smartdot.com.cn
 * @Copyright 1998-2021 © Smartdot Technologies Co., Ltd.
 */
package feizhou.untils.tools.json.bean;


import lombok.Data;

import java.util.Map;


/**
 * 字段映射配置
 *
 * @author <a href="920786312@qq.com">周飞</a>
 * @return
 * @since 2023/11/8 11:27
 */
@Data
public class FieldMappingBean implements Comparable<FieldMappingBean> {


    /**
     * 字段名称
     */
    private String fieldName;
    /**
     * 字段格式
     */
    private String fieldFormat;
    /**
     * 字段默认值，如果映射字段不存在，或者映射字段没有值，就使用默认值
     */
    private String fieldDefaultValue;
    /**
     * 映射字段名称
     */
    private String mappingFieldName;
    /**
     * 映射字段格式
     */
    private String mappingFieldFormat;

    /**
     * 映射字段值处理策略
     */
    private FieldStrategy fieldStrategy;

    /**
     * 字段层级
     */
    private Integer fieldLevel;
    /**
     * 映射字段层级
     */
    private Integer mappingFieldLevel;

    /**
     * 树节点全路径名称
     */
    private String treeFullPathName;

    /**
     * 初始化层级
     */
    public void initLevel() {
        fieldLevel = fieldName != null ? fieldName.split("\\.").length : 0;
        mappingFieldLevel = mappingFieldName != null ? mappingFieldName.split("\\.").length : 0;
    }


    public FieldMappingBean(String fieldName, String fieldDefaultValue, String mappingFieldName, FieldStrategy fieldStrategy,String fieldFormat,String mappingFieldFormat) {
        this.fieldName = fieldName;
        this.fieldDefaultValue = fieldDefaultValue;
        this.mappingFieldName = mappingFieldName;
        this.fieldStrategy = fieldStrategy;
        this.fieldFormat = fieldFormat;
        this.mappingFieldFormat = mappingFieldFormat;
        initLevel();
    }

    public FieldMappingTree buildFieldMappingTree(Map<String, FieldMappingTree> containerMap) {


        FieldMappingTree fieldMappingTree = new FieldMappingTree();

        //映射树 字段名称
        String treeFieldName = getFirstFieldNames(fieldName);
        //字段层级缩减
        fieldName = cutString(fieldName, treeFieldName);
        //树节点全路径名称
        if (treeFullPathName == null) {
            treeFullPathName = treeFieldName;
        } else {
            treeFullPathName = treeFullPathName + "." + treeFieldName;
        }
        //映射树 映射字段名称
        String treeMappingFieldName = null;

        //如果映射层级高于字段层级，削落映射层级，使其于字段层级相等
        if (mappingFieldLevel.intValue() > fieldLevel.intValue()) {

            FieldMappingTree tempTree = containerMap.get(treeFullPathName);
            if (tempTree != null) {
                //当前节点已存在
                treeMappingFieldName = tempTree.getMappingFieldName();
                //映射字段层级缩减
                this.mappingFieldName = cutString(this.mappingFieldName, treeMappingFieldName);

            } else {
                //相差层级
                int index = getMappingFieldLevel() - getFieldLevel();
                treeMappingFieldName = cutString(mappingFieldName, index);
                //映射字段层级缩减
                mappingFieldName = cutString(mappingFieldName, treeMappingFieldName);

            }

            //如果层级相同
        } else if (getMappingFieldLevel().intValue() == getFieldLevel().intValue()) {
            //映射树 映射字段名称
            treeMappingFieldName = getFirstFieldNames(mappingFieldName);
            //映射字段层级缩减
            mappingFieldName = cutString(mappingFieldName, treeMappingFieldName);


        }

        //构建fieldMappingTree
        fieldMappingTree.setFieldFormat(fieldFormat);
        fieldMappingTree.setMappingFieldFormat(mappingFieldFormat);
        fieldMappingTree.setFieldName(treeFieldName);
        fieldMappingTree.setFieldDefaultValue(fieldDefaultValue);
        fieldMappingTree.setFieldStrategy(fieldStrategy);
        fieldMappingTree.setMappingFieldName(treeMappingFieldName);
        fieldMappingTree.setFullPathName(treeFullPathName);

        initLevel();//重新构建层级
        return fieldMappingTree;

    }

    /**
     * . 分割路径名称.获取第一个
     *
     * @param pathNames
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/11/10 19:29
     */
    public String getFirstFieldNames(String pathNames) {

        int index = pathNames.indexOf(".");


        //没有逗号的场景
        if (index == -1) {
            return pathNames;
        }
        //返回切割的第1个
        return pathNames.substring(0, index);

    }

    /**
     * 切割字符串
     *
     * @param srcStr 原字符串
     * @param cutStr 要被切割的字符串
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/11/10 19:43
     */
    public String cutString(String srcStr, String cutStr) {

        if (srcStr.length() == cutStr.length()) {
            return null;
        }
        //多切一个 逗号.
        return srcStr.substring(cutStr.length() + 1);
    }

    /**
     * 切割字符串，按照逗号分割，取 index 之前的字段,包含index
     *
     * @param srcStr 原字符串
     * @param index  第几个数组之后
     * @return java.lang.String
     * @author <a href="920786312@qq.com">周飞</a>
     * @since 2023/11/10 19:43
     */
    public String cutString(String srcStr, Integer index) {
        //字符串分组
        String[] srcStrArr = srcStr.split("\\.");
        //分组大小
        int length = srcStrArr.length;
        //index 数据不合法，直接返回srcStr
        if (length < index) {
            return srcStr;
        }
        // 获取 i=index-1，.... i=0,等元素
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i <= index; i++) {
            sb.append(srcStrArr[i] + ".");
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

    @Override
    public int compareTo(FieldMappingBean other) {
        //排序，按照fieldLevel升序
        return this.fieldLevel - other.getFieldLevel();
    }
}
